package com.bwie.auth.consumer;

import cn.hutool.core.util.RandomUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


import java.util.concurrent.TimeUnit;

@Component
@Log4j2
public class AuthConsumer {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RabbitListener(queuesToDeclare = {@Queue("sendMsg")})
    public void receiveMsg(String msg, Message message, Channel channel){
        String code= RandomUtil.randomNumbers(4);
        redisTemplate.opsForValue().set("phone:"+msg,code,5, TimeUnit.MINUTES);
        log.info("注册成功验证码是:"+code);
    }

    @RabbitListener(queuesToDeclare = {@Queue("sendMsg")})
    public void receiveMsg2(String msg, Message message, Channel channel){
        String code= RandomUtil.randomNumbers(4);
        redisTemplate.opsForValue().set("phone:"+msg,code,1, TimeUnit.MINUTES);
        log.info("mq发送验证码:"+code);
    }

}
